/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package android.entidades;

import android.app.ContextoAndroid;
import android.app.Mensaje;
import android.content.Context;
import android.content.ContextWrapper;
import android.web.async.ImageDownloader;
import android.web.service.IPServer;
import android.widget.ImageView;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;

/**
 *
 * @author payo
 */
public class Usuario {
    public static String id="";
    private static String path = "";
    private static String userFile = "usuarioSVTEC.txt";
    
    public String correo,password,nombre,foto;
    public int tipo;
    
    public Usuario(String correo, String password, String nombre, String foto, int tipo) {
        this.correo = correo;
        this.password = password;
        this.nombre = nombre;
        this.foto = foto;
        this.tipo = tipo;
        saveUser(this);
    }
    
    public Usuario()
    {
        
    }
    
    public static Usuario isUser()
    {
        ContextWrapper cw = new ContextWrapper(ContextoAndroid.context);
        File directory = cw.getDir("media", Context.MODE_PRIVATE);
        path = directory.getAbsolutePath()+"/"+userFile;
        try {
            File myFile = new File(path);
            FileInputStream fIn = new FileInputStream(myFile);
            BufferedReader myReader = new BufferedReader(new InputStreamReader(fIn));
            String aDataRow = "";
            String aBuffer = "";
            while ((aDataRow = myReader.readLine()) != null) {
                    aBuffer += aDataRow;
            }
            String[] vec = aBuffer.split("&");
            Usuario u = new Usuario();
            int x=-1;
            u.correo = vec[++x];
            u.password = vec[++x];
            u.nombre = vec[++x];
            u.foto = vec[++x];
            u.tipo = Integer.parseInt(vec[++x]);
            myReader.close();
            return u;
        } 
        catch (Exception e) 
        {
            return null;
        }
    }
    
    public static void saveUser(Usuario usuario) 
    {
        ContextWrapper cw = new ContextWrapper(ContextoAndroid.context);
        File directory = cw.getDir("media", Context.MODE_PRIVATE);
        try
        {
            path = directory.getAbsolutePath()+"/"+userFile;
            FileWriter logWriter = new FileWriter(path);                  
            BufferedWriter out = new BufferedWriter(logWriter); 
            String row = usuario.correo+"&"+usuario.password+"&"+usuario.nombre+"&"+usuario.foto+"&"+usuario.tipo;
            out.write(row);
            out.close();
            } 
        catch (Exception e) {
          Mensaje.Mostrar(e.toString());
        }
    }
    
    public static void deleteUser() 
    {
        ContextWrapper cw = new ContextWrapper(ContextoAndroid.context);
        File directory = cw.getDir("media", Context.MODE_PRIVATE);
        try
        {
            path = directory.getAbsolutePath()+"/"+userFile;
            File myFile = new File(path);
            myFile.delete();
            id = "";
        } 
        catch (Exception e) {
          Mensaje.Mostrar(e.toString());
        }
    }
    
    public static void changeImage(ImageView img)
    {
        String http = IPServer.fullServer+"Fotos/uploads/thumbs/"+ContextoAndroid.usuario.foto;
        new ImageDownloader(img).execute(http);
    }    
}